package com.filetransfer.app.ui.upload;
import android.app.Dialog;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.os.Bundle;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.filetransfer.api.ShareCollection;
import com.filetransfer.app.R;
public class ShareResultDialog extends Dialog {
    private final ShareCollection.ShareResult shareResult;
    private final OnDismissListener onDismissListener;
    public ShareResultDialog(Context context, ShareCollection.ShareResult result, @Nullable OnDismissListener onDismissListener) {
        super(context);
        this.shareResult = result;
        this.onDismissListener = onDismissListener;
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.share_result);
        TextView resultTextView = findViewById(R.id.shareCodeTextView);
        ImageView copyButtonLayout = findViewById(R.id.copyIcon);
        RecyclerView filesRecyclerView = findViewById(R.id.filesRecyclerView);
        LinearLayout finishButtonLayout = findViewById(R.id.finishButtonLayout);
        var shareCode = this.shareResult.share().getCode();
        resultTextView.setText(shareCode);
        copyButtonLayout.setOnClickListener(v -> {
            ClipboardManager clipboard = (ClipboardManager) getContext().getSystemService(Context.CLIPBOARD_SERVICE);
            ClipData clip = ClipData.newPlainText("分享码", shareCode);
            clipboard.setPrimaryClip(clip);
            Toast.makeText(getContext(), "分享码 " + shareCode + " 已复制到剪切板", Toast.LENGTH_SHORT).show();
        });
        finishButtonLayout.setOnClickListener(v -> dismiss());
        filesRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        LinksAdapter adapter = new LinksAdapter(getContext(), this.shareResult.links());
        filesRecyclerView.setAdapter(adapter);
        if (onDismissListener != null) {
            setOnDismissListener(onDismissListener);
        }
    }
}
